package com.example.fengyungo_user.utils;

import android.app.Activity;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 一个专门的集合类对所有的活动（ Activity）进行管理
 *
 * @author 徐
 */
public class ActivityCollector {
    private static final String TAG = "ActivityCollector";

    public static List<Activity> activities = new ArrayList<>();

    /**
     * 添加Activity
     *
     * @param activity 添加的Activity对象
     */
    public static void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 删除Activity
     *
     * @param activity 删除的Activity对象
     */
    public static void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    /**
     * 关闭指定的Activity
     *
     * @param activityName 需要关闭的Activity包名类名
     */
    public static void finishOneActivity(String activityName) {
        // 在activities集合中找到类名与指定类名相同的Activity就关闭
        for (Activity activity : activities) {
            // activity的包名.类名，如：com.example.activity.MainActivity
            String name = activity.getClass().getName();
            if (name.equals(activityName)) {
                if (activity.isFinishing()) {
                    // 当前activity如果已经Finish，则只从activities清除就好了
                    activities.remove(activity);
                } else {
                    // 没有Finish则Finish
                    activity.finish();
                }
            }
        }
    }

    /**
     * 关闭指定的Activity
     *
     * @param targetActivity 需要关闭的Activity包名类名
     */
    public static void finishOneActivity(Class<?> targetActivity) {
        String targetName = targetActivity.getName();
        // 在activities集合中找到类名与指定类名相同的Activity就关闭
        for (Activity activity : activities) {
            // activity的包名.类名，如：com.example.activity.MainActivity
            String name = activity.getClass().getName();
            if (name.equals(targetName)) {
                if (activity.isFinishing()) {
                    // 当前activity如果已经Finish，则只从activities清除就好了
                    activities.remove(activity);
                } else {
                    // 没有Finish则Finish
                    activity.finish();
                }
            }
        }
    }

    /**
     * 只保留某个Activity，关闭其他所有Activity
     *
     * @param activityName 要保留的Activity类名
     */
    public static void finishOtherActivity(String activityName) {
        for (Activity activity : activities) {
            // activity的包名.类名，如：com.example.activity.MainActivity
            String name = activity.getClass().getName();
            // 只取类名
            name = name.substring(name.lastIndexOf('.') + 1);
            if (!name.equals(activityName)) {
                if (activity.isFinishing()) {
                    // 当前activity如果已经Finish，则只从activities清除就好了
                    activities.remove(activity);
                } else {
                    // 没有Finish则Finish
                    activity.finish();
                }
            }
        }
    }

    /**
     * 只保留某个Activity，关闭其他所有Activity
     *
     * @param targetActivity 要保留的Activity类名
     */
    public static void finishOtherActivity(Class<?> targetActivity) {
        String targetName = targetActivity.getName();
        for (Activity activity : activities) {
            // activity的包名.类名，如：com.example.activity.MainActivity
            String name = activity.getClass().getName();
            if (!name.equals(targetName)) {
                if (activity.isFinishing()) {
                    // 当前activity如果已经Finish，则只从activities清除就好了
                    activities.remove(activity);
                } else {
                    // 没有Finish则Finish
                    activity.finish();
                }
            }
        }
    }

    /**
     * 关闭所有Activity
     */
    public static void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        activities.clear();
    }


}